/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 05/12/2011, 00:45:10
 */
package GUI;

import Control.*;
import java.awt.Component;
import java.awt.Desktop;
import java.io.File;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 *
 * @author Cristian Lopez
 */
public class Principal extends javax.swing.JFrame
{
    private int cantidadCriterios = 0;
    private int cantidadAlternativas = 0;
    private Lista lmCriterios;
    private JList rowHeaderCriterios;
    private JList rowHeaderCriteriosXAlternativas;
    public static String[] valoresTipo = new String[]
    {
        "MAX", "MIN"
    };
    public static String[] valoresFPR = new String[]
    {
        "Verdadero Criterio", "Cuasi Criterio", "Seudo Criterio con preferencia lineal", "Level Criterio", "Criterio con preferencia lineal y área de indiferencia", "Criterio gaussiano"
    };
    public static String[] columnasCriterios = new String[]
    {
        "Peso Criterio", "Tipo", "Función de preferencia relativa", "p", "q", "σ"
    };
    public  static ArrayList<String> nombreCriterios;
    public  static ArrayList<String> nombreAlternativas;

    public Principal()
    {
        initComponents();
        groupButton();
        this.setLocationRelativeTo(null);        
        jTableCriterios.setVisible(false);
        jTableCriteriosXAlternativas.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelModelo = new javax.swing.JPanel();
        jScrollPaneCriterios = new javax.swing.JScrollPane();
        jTableCriterios = new javax.swing.JTable();
        jScrollPaneCriteriosXAlternativas = new javax.swing.JScrollPane();
        jTableCriteriosXAlternativas = new javax.swing.JTable();
        btnResolver = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnExportarSolucion = new javax.swing.JButton();
        jRBPromethee1 = new javax.swing.JRadioButton();
        jRBPromethee2 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuArhivo = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuAyuda = new javax.swing.JMenu();
        jMenuItemAcerca = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Promethee");

        jPanelModelo.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), "Datos de entrada"));

        jTableCriterios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, "MAX", "Verdadero Criterio", null, null, null},
                {null, "MAX", "Verdadero Criterio", null, null, null},
                {null, "MAX", "Verdadero Criterio", null, null, null}
            },
            new String [] {
                "Peso Criterio", "Tipo", "Función de preferencia relativa", "p", "q", "σ"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Double.class, java.lang.Object.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableCriterios.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTableCriterios.getTableHeader().setReorderingAllowed(false);
        jScrollPaneCriterios.setViewportView(jTableCriterios);
        jTableCriterios.getColumnModel().getColumn(0).setMinWidth(90);
        jTableCriterios.getColumnModel().getColumn(0).setPreferredWidth(90);
        jTableCriterios.getColumnModel().getColumn(2).setPreferredWidth(330);
        jTableCriterios.getColumnModel().getColumn(3).setHeaderValue("p");
        jTableCriterios.getColumnModel().getColumn(4).setHeaderValue("q");
        jTableCriterios.getColumnModel().getColumn(5).setHeaderValue("σ");

        jTableCriteriosXAlternativas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "A1", "A2", "A3"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableCriteriosXAlternativas.getTableHeader().setReorderingAllowed(false);
        jScrollPaneCriteriosXAlternativas.setViewportView(jTableCriteriosXAlternativas);

        btnResolver.setText("Resolver");
        btnResolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResolverActionPerformed(evt);
            }
        });

        jLabel1.setText("Promethee 1");

        jLabel2.setText("Promethee 2");

        btnExportarSolucion.setText("Exportar Solucion");
        btnExportarSolucion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportarSolucionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelModeloLayout = new javax.swing.GroupLayout(jPanelModelo);
        jPanelModelo.setLayout(jPanelModeloLayout);
        jPanelModeloLayout.setHorizontalGroup(
            jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelModeloLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelModeloLayout.createSequentialGroup()
                        .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPaneCriterios, javax.swing.GroupLayout.DEFAULT_SIZE, 735, Short.MAX_VALUE)
                            .addComponent(jScrollPaneCriteriosXAlternativas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 735, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(38, Short.MAX_VALUE))
                    .addGroup(jPanelModeloLayout.createSequentialGroup()
                        .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelModeloLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jRBPromethee2))
                            .addGroup(jPanelModeloLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jRBPromethee1)))
                        .addGap(106, 106, 106)
                        .addComponent(btnResolver, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(btnExportarSolucion, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanelModeloLayout.setVerticalGroup(
            jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelModeloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneCriteriosXAlternativas, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPaneCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelModeloLayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jRBPromethee1)
                            .addComponent(jLabel1))
                        .addGap(25, 25, 25)
                        .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jRBPromethee2)))
                    .addGroup(jPanelModeloLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanelModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnResolver, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnExportarSolucion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))))
                .addContainerGap())
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/help.gif"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenuArhivo.setText("Archivo");

        jMenuItem1.setText("Nuevo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenuArhivo.add(jMenuItem1);

        jMenuItem2.setText("Salir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenuArhivo.add(jMenuItem2);

        jMenuBar.add(jMenuArhivo);

        jMenuAyuda.setText("Ayuda");

        jMenuItemAcerca.setText("Acerca de");
        jMenuItemAcerca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAcercaActionPerformed(evt);
            }
        });
        jMenuAyuda.add(jMenuItemAcerca);

        jMenuBar.add(jMenuAyuda);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanelModelo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemAcercaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemAcercaActionPerformed
    {//GEN-HEADEREND:event_jMenuItemAcercaActionPerformed
        // TODO add your handling code here:
        Acerca a = new Acerca(this, true);
        a.setLocationRelativeTo(this);
        a.setTitle("Acerca de");
        a.setVisible(true);
    }//GEN-LAST:event_jMenuItemAcercaActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        this.nuevoProyecto();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here
        System.exit(0);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void btnResolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResolverActionPerformed

        obtenerDatos(true);
    }//GEN-LAST:event_btnResolverActionPerformed

    private void btnExportarSolucionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportarSolucionActionPerformed

        obtenerDatos(false);
    }//GEN-LAST:event_btnExportarSolucionActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Desktop escritorio = Desktop.getDesktop();
        try
        {
            //File archivo = new File("res/index.html");
            File archivo = new File("Ayuda/index.html");// Solo asi funciona en .JAR
            escritorio.open(archivo);
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "No se pudo abrir el archivo de ayuda", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Principal().setVisible(true);

            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExportarSolucion;
    private javax.swing.JButton btnResolver;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenuArhivo;
    private javax.swing.JMenu jMenuAyuda;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItemAcerca;
    private javax.swing.JPanel jPanelModelo;
    private javax.swing.JRadioButton jRBPromethee1;
    private javax.swing.JRadioButton jRBPromethee2;
    private javax.swing.JScrollPane jScrollPaneCriterios;
    private javax.swing.JScrollPane jScrollPaneCriteriosXAlternativas;
    private javax.swing.JTable jTableCriterios;
    private javax.swing.JTable jTableCriteriosXAlternativas;
    // End of variables declaration//GEN-END:variables

    /*private void setearModeloCriteriosXAlternativas()
    {
         String columnas []= new String [nombreAlternativas.size()];
        for(int i = 0 ;i < nombreAlternativas.size();i++)
        {
            columnas[i]= nombreAlternativas.get(i);
        }

        jTableCriteriosXAlternativas.setModel(new javax.swing.table.DefaultTableModel(
                columnas,nombreAlternativas.size()));
        
    }*/

    private void groupButton()
    {
        ButtonGroup bg = new ButtonGroup();
        bg.add(jRBPromethee1);
        bg.add(jRBPromethee2);
        jRBPromethee1.setSelected(true);
    }

    private void setearCriteriosXalternativas()
    {
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(SwingConstants.CENTER);
        for (int i = 0; i < jTableCriteriosXAlternativas.getColumnModel().getColumnCount(); i++)
        {
            jTableCriteriosXAlternativas.getColumnModel().getColumn(i).setCellRenderer(dtcr);
        }
    }

    private void nuevoProyecto() {
       
        final JLabel lblAlternativas = new JLabel("N° de Alternativas");
        final JTextField txtAlternativas = new JFormattedTextField();
        final JLabel lblCriterios = new JLabel("N° de Criterios");
        final JTextField txtCriterios = new JFormattedTextField();
        JOptionPane jopNuevoProyecto = new JOptionPane(new Object[]
                {
                    lblAlternativas, txtAlternativas, lblCriterios, txtCriterios
                }, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
        JDialog dialNuevoProyecto = jopNuevoProyecto.createDialog("Nuevo Proyecto - "
                + "Ingrese la cantidad de alternativas y criterios");
        dialNuevoProyecto.setVisible(true);

        Object aux = jopNuevoProyecto.getValue();
        if (aux == null)
        {
            return;
        }
        int result = (Integer) aux;

      
        
            if (result == JOptionPane.YES_OPTION)
            {
                try{
                    cantidadCriterios = (Integer.parseInt(txtCriterios.getText())) ;
                    cantidadAlternativas = (Integer.parseInt(txtAlternativas.getText())) ;
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(this,
                    "Los valores de alternativas y criterios deben ser mayores a 0", "Mal formato",JOptionPane.WARNING_MESSAGE);
                    this.nuevoProyecto();
                }
                dialNuevoProyecto.dispose();
                AltYCriterios ac = new AltYCriterios();
                ac.setLocationRelativeTo(this);
                ac.setVisible(true);
                this.inicializarGrillas();
            }
    }

    class RowHeaderRenderer extends JLabel implements ListCellRenderer
    {
        RowHeaderRenderer(JTable table)
        {
            JTableHeader header = table.getTableHeader();
            setOpaque(true);
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            setHorizontalAlignment(CENTER);
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
        }

        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus)
        {
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }

    class Lista extends AbstractListModel
    {
       
        ArrayList<String> headers = new ArrayList<String>();

        public int getSize()
        {
            return headers.size();
        }

        public Object getElementAt(int index)
        {
            return headers.get(index);
        }

        private void addCriterio()
        {
            headers.add("C" + (cantidadCriterios + 1));            
        }

        public void removeCriterio()
        {
            headers.remove(headers.size() - 1);
            cantidadCriterios--;
        }

        public void cambiarNombreCriterios(int index, String nombre)
        {
            headers.set(index, nombre);
        }

        public void clear()
        {
            headers.clear();
        }

        public void setLista()
        {
            Object data [][]= new Object[cantidadCriterios][6];
            Object data2 [][]= new Object[cantidadCriterios][cantidadAlternativas];
            String columns [] = new String[cantidadAlternativas];
            for(int i=0; i < cantidadCriterios; i++)
            {
                this.addCriterio();
                data[i][0] = null;
                data[i][1] = "MAX";
                data[i][2] = "Verdadero Criterio";
                data[i][3] = null;
                data[i][4] = null;
                data[i][5] = null;
                for(int j=0; j<cantidadAlternativas; j++)
                {
                    data2[i][j] = null;
                }
            }

            for(int i = 0; i < cantidadAlternativas; i++)
            {
                columns[i] = nombreAlternativas.get(i);
            }
            jTableCriterios.setModel(new javax.swing.table.DefaultTableModel(
            data,
            new String [] {
                "Peso Criterio", "Tipo", "Función de preferencia relativa", "p", "q", "σ"}));

            jTableCriteriosXAlternativas.setModel(new javax.swing.table.DefaultTableModel(
            data2,columns));
        }
    }

    public class MyComboBoxEditor extends DefaultCellEditor
    {
        public MyComboBoxEditor(String[] items)
        {
            super(new JComboBox(items));
        }
    }

    public void cargarCriterios(double[][] matrizCriterios, ArrayList<String> nCr)
    {
        DefaultTableModel dm = (DefaultTableModel) jTableCriterios.getModel();
        for (int i = 0; i < matrizCriterios.length; i++)
        {
            for (int h = 0; h < matrizCriterios[0].length; h++)
            {
                if (matrizCriterios[i][h] >= 0)
                {
                    if (h == 1)
                    {
                        if (matrizCriterios[i][h] > 0)
                        {
                            dm.setValueAt(valoresTipo[(int) (matrizCriterios[i][h] - 1)], i, h);
                        }
                    }
                    else if (h == 2)
                    {
                        if (matrizCriterios[i][h] > 0)
                        {
                            dm.setValueAt(valoresFPR[(int) (matrizCriterios[i][h] - 1)], i, h);
                        }
                    }
                    else
                    {
                        dm.setValueAt(matrizCriterios[i][h], i, h);
                    }
                }
            }
        }
        nombreCriterios = nCr;
        cambiarNombresCriterios();
    }

    public void cargarCriteriosXAlternativas(double[][] matrizCriteriosXAlternativas, boolean criteriosXAlternativas, ArrayList<String> nAl)
    {
        DefaultTableModel dm = (DefaultTableModel) jTableCriteriosXAlternativas.getModel();
        for (int i = 0; i < matrizCriteriosXAlternativas.length; i++)
        {
            for (int h = 0; h < matrizCriteriosXAlternativas[0].length; h++)
            {
                if (matrizCriteriosXAlternativas[i][h] >= 0)
                {
                    dm.setValueAt(matrizCriteriosXAlternativas[i][h], i, h);
                }
            }
        }
        nombreAlternativas = nAl;
        cambiarNombresAlternativas();
    }

    public void cambiarNombresAlternativas()
    {
        String[] nombres = new String[nombreAlternativas.size()];
//        String nombre;
        for (int i = 0; i < nombreAlternativas.size(); i++)
        {
            nombres[i] = nombreAlternativas.get(i);
        }
        DefaultTableModel dm = (DefaultTableModel) jTableCriteriosXAlternativas.getModel();
        dm.setColumnIdentifiers(nombres);
        setearCriteriosXalternativas();
    }

    public void cambiarNombresCriterios()
    {
        for (int i = 0; i < nombreCriterios.size(); i++)
        {
            lmCriterios.cambiarNombreCriterios(i, nombreCriterios.get(i));
        }
        rowHeaderCriterios.setModel(lmCriterios);
        jScrollPaneCriterios.repaint();
        rowHeaderCriteriosXAlternativas.setModel(lmCriterios);
        jScrollPaneCriteriosXAlternativas.repaint();
    }

    private void obtenerDatos(boolean resolver)
    {
        
            double val[][] = new double[jTableCriteriosXAlternativas.getColumnCount()][jTableCriteriosXAlternativas.getRowCount()];
            double pesos[] = new double[jTableCriterios.getRowCount()];
            int tipo[] = new int[jTableCriterios.getRowCount()];
            int funcion[] = new int[jTableCriterios.getRowCount()];
            double p[] = new double[jTableCriterios.getRowCount()];
            double q[] = new double[jTableCriterios.getRowCount()];
            double sigma[] = new double[jTableCriterios.getRowCount()];
            String aux;
            double ex;

            try
            {
                for (int i = 0; i < jTableCriterios.getRowCount(); i++)
                {
                    for (int j = 0; j < jTableCriterios.getColumnCount(); j++)
                    {
                        aux = String.valueOf(jTableCriterios.getValueAt(i, j));
                        if (j < 1 || j > 2)
                        {
                            try
                            {
                                switch (j)
                                {
                                    case 0:
                                        ex = Double.parseDouble(aux);
                                        if (ex >= 0)
                                        {
                                            pesos[i] = ex;
                                        }
                                        else
                                        {
                                            throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + (j + 1) + " en la tabla de criterios debe ser un número decimal mayor o igual a 0");
                                        }
                                        break;
                                    case 3:
                                        if (funcion[i] == 3 || funcion[i] == 4 || funcion[i] == 5)
                                        {
                                            ex = Double.parseDouble(aux);
                                            if (ex >= 0)
                                            {
                                                p[i] = ex;
                                            }
                                            else
                                            {
                                                throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + (j + 1) + " en la tabla de criterios debe ser un número decimal mayor o igual a 0");
                                            }
                                        }
                                        break;
                                    case 4:
                                        if (funcion[i] == 2 || funcion[i] == 4 || funcion[i] == 5)
                                        {
                                            ex = Double.parseDouble(aux);
                                            if (ex >= 0)
                                            {
                                                q[i] = ex;
                                            }
                                            else
                                            {
                                                throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + (j + 1) + " en la tabla de criterios debe ser un número decimal mayor o igual a 0");
                                            }
                                        }
                                        break;
                                    case 5:
                                        if (funcion[i] == 6)
                                        {
                                            ex = Double.parseDouble(aux);
                                            if (ex >= 0)
                                            {
                                                sigma[i] = ex;
                                            }
                                            else
                                            {
                                                throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + columnasCriterios[j] + " en la tabla de criterios debe ser un número decimal mayor o igual a 0");
                                            }
                                        }
                                        break;
                                }
                            }
                            catch (Exception e)
                            {
                                throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + columnasCriterios[j] + " en la tabla de criterios debe ser un número decimal mayor o igual a 0");
                            }

                        }
                        else
                        {
                            switch (j)
                            {
                                case 1:
                                    for (int k = 0; k < valoresTipo.length; k++)
                                    {
                                        if (aux.compareTo(valoresTipo[k]) == 0)
                                        {
                                            tipo[i] = k;
                                            break;
                                        }
                                    }
                                    break;
                                case 2:
                                    for (int k = 0; k < valoresFPR.length; k++)
                                    {
                                        if (aux.compareTo(valoresFPR[k]) == 0)
                                        {
                                            funcion[i] = (k + 1);
                                            break;
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                }
                for (int i = 0; i < jTableCriteriosXAlternativas.getRowCount(); i++)
                {
                    for (int j = 0; j < jTableCriteriosXAlternativas.getColumnCount(); j++)
                    {
                        try
                        {
                            aux = String.valueOf(jTableCriteriosXAlternativas.getValueAt(i, j));
                            ex = Double.parseDouble(aux);
                            if (ex >= 0)
                            {
                                val[j][i] = ex;
                            }
                            else
                            {
                                throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + (j + 1) + " en la tabla de criterios por alternativas debe ser un número decimal mayor o igual a 0");
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("El valor de la celda en la fila " + (i + 1) + " y la columna " + (j + 1) + " en la tabla de criterios por alternativas debe ser un número decimal mayor o igual a 0");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(Principal.this, e.getMessage(), "Error resolver el problema", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String[] criterios = new String[nombreCriterios.size()];
            for (int i = 0; i < nombreCriterios.size(); i++)
            {
                aux = nombreCriterios.get(i);
                if (aux.compareTo("") == 0)
                {
                    criterios[i] = "C" + (i + 1);
                }
                else
                {
                    criterios[i] = aux;
                }
            }
            String[] alternativas = new String[nombreAlternativas.size()];
            for (int i = 0; i < nombreAlternativas.size(); i++)
            {
                aux = nombreAlternativas.get(i);
                if (aux.compareTo("") == 0)
                {
                    alternativas[i] = "A" + (i + 1);
                }
                else
                {
                    alternativas[i] = aux;
                }
            }

            int tipoPromethee;
            if (jRBPromethee1.isSelected())
            {
                tipoPromethee = 1;
            }
            else
            {
                tipoPromethee = 2;
            }


           GestorProcesamiento gestor = new GestorProcesamiento(criterios, alternativas, val, pesos, tipo, funcion, p, q, sigma, tipoPromethee);
            gestor.resolver();
            gestor.armarGrafo();

            if (resolver)
            {
                Resultado resultado = new Resultado(criterios, alternativas, gestor, this);
                this.setVisible(false);
                resultado.setVisible(true);
            }
            else
            {
                Wizard wizard = new Wizard(gestor, criterios, alternativas, this);
                wizard.setVisible(true);
                this.setVisible(false);
            }
        
    }

    public void setNombreAlternativas (ArrayList<String> nombAlt){
        nombreAlternativas = nombAlt;
    }

    public void setNombreCriterios (ArrayList<String> nombCri){
        nombreAlternativas = nombCri;
    }

    public void inicializarGrillas()
    {
        jTableCriterios.setVisible(true);
        jTableCriteriosXAlternativas.setVisible(true);

       

        jTableCriterios.setRowHeight(20);
        jTableCriteriosXAlternativas.setRowHeight(20);
        jTableCriteriosXAlternativas.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        lmCriterios = new Lista();
        lmCriterios.setLista();

        rowHeaderCriterios = new JList(lmCriterios);
        rowHeaderCriterios.setFixedCellWidth(100);
        rowHeaderCriterios.setBackground(jTableCriterios.getTableHeader().getBackground());
        rowHeaderCriterios.setFixedCellHeight(jTableCriterios.getRowHeight());
        rowHeaderCriterios.setCellRenderer(new RowHeaderRenderer(jTableCriterios));
        jScrollPaneCriterios.setRowHeaderView(rowHeaderCriterios);

        rowHeaderCriteriosXAlternativas = new JList(lmCriterios);
        rowHeaderCriteriosXAlternativas.setFixedCellWidth(100);
        rowHeaderCriteriosXAlternativas.setBackground(jTableCriteriosXAlternativas.getTableHeader().getBackground());
        rowHeaderCriteriosXAlternativas.setFixedCellHeight(jTableCriteriosXAlternativas.getRowHeight());
        rowHeaderCriteriosXAlternativas.setCellRenderer(new RowHeaderRenderer(jTableCriteriosXAlternativas));
        jScrollPaneCriteriosXAlternativas.setRowHeaderView(rowHeaderCriteriosXAlternativas);

        jTableCriterios.getColumnModel().getColumn(1).setCellEditor(new MyComboBoxEditor(valoresTipo));
        jTableCriterios.getColumnModel().getColumn(2).setCellEditor(new MyComboBoxEditor(valoresFPR));
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(SwingConstants.CENTER);
        for (int i = 0; i < jTableCriterios.getColumnModel().getColumnCount(); i++)
        {
            jTableCriterios.getColumnModel().getColumn(i).setCellRenderer(dtcr);
        }
        for (int i = 0; i < jTableCriteriosXAlternativas.getColumnModel().getColumnCount(); i++)
        {
            jTableCriteriosXAlternativas.getColumnModel().getColumn(i).setCellRenderer(dtcr);
        }
    }
    

//CLASE INTERNA PARA CAMBIAR LOS NOMBRE DE LAS ALTERNATIVAS Y CRITERIOS

    class AltYCriterios extends javax.swing.JFrame
    {
             /** Creates new form AltYCriterios */
        public AltYCriterios() {
            nombreAlternativas = new ArrayList<String>();
            nombreCriterios = new ArrayList<String>();
            initComponents();
            setearNombres();
            cargarComboAlternativas(0);
            cargarComboCriterios(0);
            
        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {

            jPanelAlternativasyCriterios = new javax.swing.JPanel();
            comboAlternativas = new javax.swing.JComboBox();
            jLabel1 = new javax.swing.JLabel();
            txtNombreAlternativa = new javax.swing.JTextField();
            btnCambiarNombreAlternativa = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            comboCriterios = new javax.swing.JComboBox();
            jLabel2 = new javax.swing.JLabel();
            txtNombreCriterio = new javax.swing.JTextField();
            btnCambiarNombreCriterio = new javax.swing.JButton();
            btnAceptar = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Aternativas y Criterios");

            jPanelAlternativasyCriterios.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Alternativas"));
            jPanelAlternativasyCriterios.setToolTipText("");

            comboAlternativas.setToolTipText("");
            comboAlternativas.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    comboAlternativasActionPerformed(evt);
                }
            });

            jLabel1.setText("Nombre :");

            btnCambiarNombreAlternativa.setText("Cambiar");
            btnCambiarNombreAlternativa.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCambiarNombreAlternativaActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanelAlternativasyCriteriosLayout = new javax.swing.GroupLayout(jPanelAlternativasyCriterios);
            jPanelAlternativasyCriterios.setLayout(jPanelAlternativasyCriteriosLayout);
            jPanelAlternativasyCriteriosLayout.setHorizontalGroup(
                jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                            .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(comboAlternativas, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtNombreAlternativa, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                            .addContainerGap(24, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                            .addComponent(btnCambiarNombreAlternativa)
                            .addGap(72, 72, 72))))
            );
            jPanelAlternativasyCriteriosLayout.setVerticalGroup(
                jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(comboAlternativas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(27, 27, 27)
                    .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtNombreAlternativa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(btnCambiarNombreAlternativa)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Criterios")));
            jPanel1.setToolTipText("");

            comboCriterios.setToolTipText("");
            comboCriterios.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    comboCriteriosActionPerformed(evt);
                }
            });

            jLabel2.setText("Nombre :");

            btnCambiarNombreCriterio.setText("Cambiar");
            btnCambiarNombreCriterio.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCambiarNombreCriterioActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(comboCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtNombreCriterio, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                            .addContainerGap(23, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(btnCambiarNombreCriterio)
                            .addGap(71, 71, 71))))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(comboCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(27, 27, 27)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtNombreCriterio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(btnCambiarNombreCriterio)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            btnAceptar.setText("Aceptar");
            btnAceptar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAceptarActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanelAlternativasyCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanelAlternativasyCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>

        private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
            cambiarNombresAlternativas();
            cambiarNombresCriterios();
            this.dispose();
        }

        private void btnCambiarNombreAlternativaActionPerformed(java.awt.event.ActionEvent evt) {
            cambiarNombreAlternativa(txtNombreAlternativa,comboAlternativas);
        }

        private void btnCambiarNombreCriterioActionPerformed(java.awt.event.ActionEvent evt) {
            cambiarNombreCriterio(txtNombreCriterio, comboCriterios);
        }

        private void comboAlternativasActionPerformed(java.awt.event.ActionEvent evt) {
            mostrarNombreAlternativa(txtNombreAlternativa, comboAlternativas);
        }

        private void comboCriteriosActionPerformed(java.awt.event.ActionEvent evt) {
            mostrarNombreCriterio(txtNombreCriterio, comboCriterios);
        }

        // Variables declaration - do not modify
        private javax.swing.JButton btnAceptar;
        private javax.swing.JButton btnCambiarNombreAlternativa;
        private javax.swing.JButton btnCambiarNombreCriterio;
        private javax.swing.JComboBox comboAlternativas;
        private javax.swing.JComboBox comboCriterios;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanelAlternativasyCriterios;
        private javax.swing.JTextField txtNombreAlternativa;
        private javax.swing.JTextField txtNombreCriterio;

        private void cargarComboAlternativas(int indice){

            if(comboAlternativas.getItemCount()>0)
            {
                comboAlternativas.removeAllItems();
            }

            for (int i = 0; i < cantidadAlternativas; i++)
            {
                comboAlternativas.addItem(nombreAlternativas.get(i));
            }

            comboAlternativas.setSelectedIndex(indice);
            txtNombreAlternativa.setText(nombreAlternativas.get(indice));
        }

         private void cargarComboCriterios(int indice){

            if(comboCriterios.getItemCount()>0)
            {
                comboCriterios.removeAllItems();
            }

            for (int i = 0; i < nombreCriterios.size(); i++)
            {
                comboCriterios.addItem(nombreCriterios.get(i));
            }
            comboCriterios.setSelectedIndex(indice);
            txtNombreCriterio.setText(nombreCriterios.get(indice));
        }

        private void cambiarNombreAlternativa(JTextField jtf, JComboBox jcb)
        {
            if(jtf.getText().compareTo("")!=0){
                nombreAlternativas.set(jcb.getSelectedIndex(), jtf.getText());
            }
            else
            {
                JOptionPane.showMessageDialog(this,
            "El nombre no puede estra vacio", "Mal formato",JOptionPane.WARNING_MESSAGE);
                        return;
            }

            cargarComboAlternativas(jcb.getSelectedIndex());

        }

        private void cambiarNombreCriterio(JTextField jtf, JComboBox jcb)
        {
            if(jtf.getText().compareTo("")!=0){
                nombreCriterios.set(jcb.getSelectedIndex(), jtf.getText());
            }
            else
            {
                JOptionPane.showMessageDialog(this,
            "El nombre no puede estra vacio", "Mal formato",JOptionPane.WARNING_MESSAGE);
                        return;
            }

            cargarComboCriterios(jcb.getSelectedIndex());
        }

        private void setearNombres()
        {
            for (int i = 0; i < cantidadAlternativas; i++)
            {
                nombreAlternativas.add("Alternativa"+(i+1));
            }

            for (int i = 0; i < cantidadCriterios; i++)
            {
                nombreCriterios.add("Criterio"+(i+1));
            }
        }

         private void mostrarNombreAlternativa(JTextField jtf, JComboBox jcb)
        {
             if(jcb.getItemCount()>0)
             {
              jtf.setText(nombreAlternativas.get(jcb.getSelectedIndex()));// cuando cambio un nombre se llama y trae index -1
            }

        }

          private void mostrarNombreCriterio(JTextField jtf, JComboBox jcb)
        {
              if(jcb.getItemCount()>0)
             {
                 jtf.setText(nombreCriterios.get(jcb.getSelectedIndex()));
             }
        }       
        
    }
}
